﻿using NConf;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using NConf.Interfaces;
using System.Collections.Generic;

using NConf.ConfigSources;

namespace NConfTests
{
    [TestClass()]
    public class ConfigManagerTest
    {
        #region TestContext

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #endregion

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        [TestMethod()]
        public void AddConfigSourceTest()
        {
            ConfigManager confman = new ConfigManager();
            IConfigSource source = new DictConfigSource();
            source.Configuration["test"] = "foo";
            confman.AddConfigSource(source);

            Assert.AreEqual("foo", confman.Configuration["test"]);
        } // end AddConfigSourceTest

        [TestMethod()]
        public void GetSourceProvidingKeyTest()
        {
            ConfigManager confman = new ConfigManager();

            IConfigSource source1 = new DictConfigSource();
            source1.Configuration["test"] = new Dictionary<string, object> { {"foo", "bar"} };
            source1.Configuration["bar"] = "baz";
            source1.Configuration["cats"] = "apples";
            
            IConfigSource source2 = new DictConfigSource();
            source2.Configuration["test"] = "foo";
            source2.Configuration["bar"] = "foo";
            source2.Configuration["dogs"] = "oranges";

            IConfigSource source3 = new DictConfigSource();
            source3.Configuration["test"] = "foo";
            source3.Configuration["xyz"] = "abc";
            source3.Configuration["dogs"] = "apples";

            confman.AddConfigSource(source1);
            confman.AddConfigSource(source3);
            confman.AddConfigSource(source2);

            var source = confman.GetSourceProvidingKey("dogs");
            var source_1 = confman.GetSourceProvidingKey("sogs");

            Assert.AreEqual(source3, source);
            Assert.IsNull(source_1);
        } // end GetSourceProvidingKeyTest

        [TestMethod()]
        public void MultipleConfigSourceTest()
        {
            ConfigManager confman = new ConfigManager();

            IConfigSource source1 = new DictConfigSource();
            source1.Configuration["test"] = new Dictionary<string, object> { {"foo", "bar"} };
            source1.Configuration["bar"] = "baz";
            source1.Configuration["cats"] = "apples";
            
            IConfigSource source2 = new DictConfigSource();
            source2.Configuration["test"] = "foo";
            source2.Configuration["bar"] = "foo";
            source2.Configuration["dogs"] = "oranges";

            IConfigSource source3 = new DictConfigSource();
            source3.Configuration["test"] = "foo";
            source3.Configuration["xyz"] = "abc";
            source3.Configuration["dogs"] = "apples";

            confman.AddConfigSource(source1);
            confman.AddConfigSource(source3);
            confman.AddConfigSource(source2);

            var test = confman.IsSection("test");
            var foo = confman.GetValue("test.foo");
            var bar = confman.GetValue("bar");
            var dogs = confman.GetValue("dogs");
            var cats = confman.GetValue("cats");
            var xyz = confman.GetValue("xyz");

            Assert.AreEqual(true, test);
            Assert.AreEqual("bar", foo);
            Assert.AreEqual("baz", bar);
            Assert.AreEqual("apples", dogs);
            Assert.AreEqual("apples", cats);
            Assert.AreEqual("abc", xyz);
        } // end MultipleConfigSourceTest

        [TestMethod()]
        public void GetConfigKeyTest()
        {
            ConfigManager confman = new ConfigManager();
            IConfigSource source = new DictConfigSource();
            source.Configuration["test"] = "foo";
            confman.AddConfigSource(source);

            var configKey = confman.GetConfigKey("test");

            Assert.AreEqual("foo", configKey);
        } // end GetConfigKeyTest

        [TestMethod()]
        public void GetConfigKeyComplexTest()
        {
            ConfigManager confman = new ConfigManager();
            IConfigSource source = new DictConfigSource();
            source.Configuration["test"] = new Dictionary<string, object> { {"foo", "bar"} };
            confman.AddConfigSource(source);

            var configKey = confman.GetConfigKey("test.foo");

            Assert.AreEqual("bar", configKey);
        } // end GetConfigKeyComplexTest

        [TestMethod()]
        public void GetValueTest()
        {
            ConfigManager confman = new ConfigManager();
            IConfigSource source = new DictConfigSource();
            source.Configuration["test"] = new Dictionary<string, object> { {"foo", "bar"} };
            confman.AddConfigSource(source);

            var results = confman.GetValue("test.foo");

            Assert.AreEqual("bar", results);
        } // GetValueTest

        [TestMethod()]
        public void GetValueWithDefaultTest()
        {
            ConfigManager confman = new ConfigManager();
            IConfigSource source = new DictConfigSource();
            source.Configuration["test"] = new Dictionary<string, object> { {"foo", "bar"} };
            confman.AddConfigSource(source);

            var results = confman.GetValue("test.foo");
            var defaultVal = confman.GetValue("baz.bleh.21", null);

            Assert.AreEqual("bar", results);
            Assert.IsNull(defaultVal);
        } // GetValueWithDefaultTest

        [TestMethod()]
        public void HasValueTest()
        {
            ConfigManager confman = new ConfigManager();
            IConfigSource source = new DictConfigSource();
            source.Configuration["test"] = new Dictionary<string, object> { {"foo", "bar"} };
            confman.AddConfigSource(source);

            var results = confman.HasValue("test.foo");
            var results1 = confman.HasValue("test");
            var results2 = confman.HasValue("qqqq");

            Assert.AreEqual(true, results);
            Assert.AreEqual(true, results1);
            Assert.AreEqual(false, results2);
        } // end HasValueTest

        [TestMethod()]
        public void IsSectionTest()
        {
            ConfigManager confman = new ConfigManager();
            IConfigSource source = new DictConfigSource();
            source.Configuration["test"] = new Dictionary<string, object> { {"foo", "bar"} };
            confman.AddConfigSource(source);

            var results = confman.IsSection("test.foo");
            var results1 = confman.IsSection("test");

            Assert.AreEqual(false, results);
            Assert.AreEqual(true, results1);
        } // end IsSectionTest

        [TestMethod()]
        public void RemoveConfigSourceTest()
        {
            ConfigManager confman = new ConfigManager();
            IConfigSource source = new DictConfigSource();
            source.Configuration["test"] = new Dictionary<string, object> { {"foo", "bar"} };
            confman.AddConfigSource(source);

            var results = confman.HasValue("test.foo");
            Assert.AreEqual(true, results);

            confman.RemoveConfigSource(source);

            var results1 = confman.HasValue("test.foo");
            Assert.AreEqual(false, results1);
        } // end RemoveConfigSourceTest
    } // end ConfigManagerTest
} // end NConfTests namepspace
